package com.ryskoo.controller.vendormanager;

import com.ryskoo.common.hclconstant.EmployeeTypeEnum;
import com.ryskoo.customer.vendor.domain.VendorPO;
import com.ryskoo.customer.vendormanager.domain.VendorManagerPO;
import com.ryskoo.customer.vendormanager.service.IVendorManagerService;
import com.ryskoo.framework.core.bean.*;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.exception.InvalidParamException;
import com.ryskoo.framework.core.utils.LoginUtil;
import com.ryskoo.framework.core.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * HCL商户员工管理
 * @author BeHappy
 */
@Controller
@RequestMapping("/vendor/vendormanager")
public class VendorManagerController extends BasicHandler<VendorManagerPO> {
    private static final String TEMPLATE_PATH = "system/vendormanager";

    private static final String MODULE_NAME = "VendorManager";
    /**
     * 基类构造方法.
     */
    public VendorManagerController() {
        super(TEMPLATE_PATH, MODULE_NAME);
    }

    private IVendorManagerService vendorManagerService;

    /**
     * 消费者注入
     *
     * @param vendorManagerService 消费者基类
     */
    @Autowired
    protected void setService(IVendorManagerService vendorManagerService) {
        this.vendorManagerService = vendorManagerService;
        super.setService(vendorManagerService);
    }

    /**
     * 构建查询条件
     * @param qm
     */
    @Override
    protected void buildQueryMessage(QueryMessageDTO qm) {
        qm.addCondition("vendorUuid", QueryConditionEnum.EQ.name(), LoginUtil.getLoginVendorUuid());
    }

    @Override
    protected void buildListPageData(Model model, HttpServletRequest request) {
        buildEmpType(model);
    }

    protected void buildAddPageData(Model model, HttpServletRequest request) {
        buildEmpType(model);
    }
    protected void buildEditPageData(Model model, VendorManagerPO po, HttpServletRequest request ) {
        buildEmpType(model);
    }

    private void buildEmpType(Model model) {
        List<EnumConvertParam> employeeTypeList = new ArrayList<>();
        for (EmployeeTypeEnum ose : EmployeeTypeEnum.values()) {
            EnumConvertParam param = new EnumConvertParam();
            param.setValue(ose.getValue());
            param.setName(ose.getName());
            employeeTypeList.add(param);
        }
        model.addAttribute("employeeTypeList", employeeTypeList);
    }

    @PostMapping("/add")
    @ResponseBody
    public ReturnMessageDTO add(@RequestBody VendorManagerPO po) {
        VendorPO vendor = LoginUtil.getLoginVendorModel();
        po.setVendorUuid(vendor.getUuid());
        return super.add(po);
    }

    /**
     * 修改密码
     *
     * @param uuid
     * @param password
     * @return 统一返回消息 {@link ReturnMessageDTO}
     */
    @PostMapping("/resetPassword")
    @ResponseBody
    public ReturnMessageDTO resetPassword(@RequestParam("uuid") String uuid, @RequestParam("password") String password) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        try {
            VendorManagerPO po = vendorManagerService.getByUuid(uuid);
            po.setPassword(password);
            vendorManagerService.modifyPassword(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("修改密码成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("修改密码失败");
        }
        return returnMessage;
    }

    /**
     *
     * 商户中心头部修改密码
     *
     * @param oldPwd
     * @param pwd
     * @param confirmPwd
     * @return ReturnMessageDTO
     * @auther linq
     * @date 2018/11/13 20:36
     */
    @PostMapping("/updatepassword")
    @ResponseBody
    public ReturnMessageDTO updatepassword(@RequestParam(value = "oldPwd") String oldPwd,
                                           @RequestParam(value = "pwd") String pwd,@RequestParam(value = "confirmPwd") String confirmPwd) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        try {
            // 密码不能为空
            if (StringUtils.isEmpty(oldPwd) || StringUtils.isEmpty(pwd) || StringUtils.isEmpty(confirmPwd)) {
                returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
                returnMessage.setReturnNote("pwd_empty");
                return returnMessage;
            }

            VendorManagerPO vendorManager = LoginUtil.getLoginUserModel();
            if (null == vendorManager) {
                returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
                returnMessage.setReturnNote("no_manager");
                return returnMessage;
            }

            // 验证旧密码是否正确
            oldPwd = MD5Util.md5(vendorManager.getLoginName() + oldPwd);
            if (!oldPwd.equals(vendorManager.getPassword())) {
                returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
                returnMessage.setReturnNote("error_pwd");
                return returnMessage;
            }

            // 验证新密码是否相同
            if (!pwd.equals(confirmPwd)) {
                returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
                returnMessage.setReturnNote("no_equal");
                return returnMessage;
            }

            vendorManager.setPassword(pwd);
            vendorManagerService.modifyPassword(vendorManager);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("修改成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("修改失败");
        }
        return returnMessage;
    }
}
